/*
 * GpFunctions.java
 *
 * Created on 31. Januar 2007, 22:54
 *
 *  This file is part of JSmex.
 *
 *  JSmex is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  JSmex is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package jsmex.function;

import java.util.Vector;
import jsmex.*;
import jsmex.function.CardApplication;
import jsmex.cardservice.GlobalPlatformCardService;
import jsmex.gui.GlobalPlatformGUI;

/**
 *
 * @author Tobias Senger
 */
public class GpFunctions {
    
    private GlobalPlatformCardService cs = null;
    private LogTracer lt = null;
    private GlobalPlatformGUI gpgui = null;
    private byte[] S_ENC = {(byte)0x40,(byte)0x41,(byte)0x42,(byte)0x43,(byte)0x44,(byte)0x45,(byte)0x46,(byte)0x47,(byte)0x48,(byte)0x49,(byte)0x4A,(byte)0x4B,(byte)0x4C,(byte)0x4D,(byte)0x4E,(byte)0x4F};
    private byte[] S_MAC = {(byte)0x40,(byte)0x41,(byte)0x42,(byte)0x43,(byte)0x44,(byte)0x45,(byte)0x46,(byte)0x47,(byte)0x48,(byte)0x49,(byte)0x4A,(byte)0x4B,(byte)0x4C,(byte)0x4D,(byte)0x4E,(byte)0x4F};
    private byte[] DEK = {(byte)0x40,(byte)0x41,(byte)0x42,(byte)0x43,(byte)0x44,(byte)0x45,(byte)0x46,(byte)0x47,(byte)0x48,(byte)0x49,(byte)0x4A,(byte)0x4B,(byte)0x4C,(byte)0x4D,(byte)0x4E,(byte)0x4F};
    
    
    /** Creates a new instance of GpFunctions */
    public GpFunctions(GlobalPlatformCardService gpcs, LogTracer ltracer) {
        cs = gpcs;
        lt = ltracer;
        showGUI();
    }
    
    /**Show the Geldkarte-GUI
     *
     */
    public void showGUI() {
        if (gpgui == null) gpgui = new GlobalPlatformGUI(this);
        lt.info("called method showGUI().", this);
        gpgui.setVisible(true);
    }
    
    /**Close the Geldkarte-GUI
     *
     */
    public void closeGUI() {
        lt.info("called method closeGUI().", this);
        gpgui.setVisible(false);
        gpgui.dispose();
        gpgui = null;
    }
    
    
    /**Returns an array of CardApplications.
     *
     * @return
     */
    public CardApplication[] getApplications() {
        
        lt.info("called method getApplications().", this);
        if (cs.mutualAuthentication(S_ENC, S_MAC)) {
            
            byte[] subset = {(byte)0x80, (byte)0x40, (byte)0x20};
            
            Vector appList = new Vector();
            for (int k=0;k<subset.length;k++) {
                byte[] response = cs.getStatus(subset[k]);
                if (response != null) {
                    StringBuffer sb = new StringBuffer();
                    byte lifeCycleState = 0;
                    byte appPrivileges = 0;
                    int i,j;
                    
                    for (i=0;i<response.length;i++) {
                        byte[] tempbyte = new byte[response[i]];
                        for (j=0;j<tempbyte.length;j++) {
                            tempbyte[j] = response[i+1+j];
                        }
                        lifeCycleState = response[i+j+1];
                        appPrivileges = response[i+j+2];
                        appList.add(new CardApplication(tempbyte,lifeCycleState,appPrivileges,subset[k]));
                        i +=j+2;
                    }
                    
                }
            }
            CardApplication[] al = new CardApplication[appList.size()];
            al = (CardApplication[]) appList.toArray(al);
            return al;
        }
        return null;
    }
    
}
